﻿using System;
using System.Linq;
using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.Process;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Process
{
    /// <summary>
    /// 工序维护信息展示处理类
    /// </summary>
    public class GetProcessHandler : IRequestHandler<GetProcessCommand, ApiPaging<ProcessDto>>
    {
        private readonly IBaseRespository<ProcessModel> processRepo;

        public GetProcessHandler(IBaseRespository<ProcessModel> processRepo)
        {
            this.processRepo = processRepo;
        }

        public Task<ApiPaging<ProcessDto>> Handle(GetProcessCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<ProcessDto> res=new ApiPaging<ProcessDto>();
            var process = processRepo.GetAll().Where(x=>!x.Isdel);
            var linq = from a in process
                       select new ProcessDto
                       {
                           Id=a.Id,
                           Processcode=a.Processcode,
                           Processname=a.Processname,
                           Processdesc=a.Processdesc,
                           ProcessType = a.ProcessType,
                           Reporting=a.Reporting,
                           CreateName=a.CreateName,
                           CreateTime=a.CreateTime,
                           UpdateTime=a.UpdateTime,
                       };
            res.PageDatas=linq.OrderByDescending(x=>x.CreateTime).Skip((request.Index-1)*request.Size).Take(request.Size).ToList();
            res.TotalCount=linq.Count();
            res.Code=ApiEnum.Success;
            res.Msg = "工序维护信息查询成功";

            return Task.FromResult(res);
        }
    }
}
